package com.uwo.house.controller;

import com.uwo.house.beans.request.DistributeDesignReq;
import com.uwo.house.beans.request.MeasureCenterReq;
import com.uwo.house.beans.response.DesignOrderResp;
import com.uwo.house.beans.response.OrderDesignDetail;
import com.uwo.house.beans.response.RelatedStuffInfo;
import com.uwo.house.beans.response.RelatedStuffResp;
import com.uwo.house.config.SmsContentConfig;
import com.uwo.house.kits.SmsKits;
import com.uwo.house.service.IDesignCenterService;
import com.uwo.house.service.IDesignOrderService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台设计订单管理
 * Created by cyn1c on 2017/7/24.
 */
@Controller
@RequestMapping("/design")
public class DesignOrderController {
    @Autowired
    IDesignOrderService designOrderService;

    @Autowired
    IDesignCenterService iDesignCenterService;

    /**
     * 管理中心-查询设计订单
     *
     * @param orderId
     * @param decorationStyle
     * @param distributeFlag
     * @param scheduleStatus
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getDesignOrderList(@RequestParam(value = "orderId", required = false) String orderId,
                                  @RequestParam(value = "decorationStyle", required = false) String decorationStyle,
                                  @RequestParam(value = "distributeFlag", required = false) String distributeFlag,
                                  @RequestParam(value = "scheduleStatus", required = false) String scheduleStatus) {
        List<DesignOrderResp> list = this.designOrderService.getDesignOrderList(orderId, decorationStyle, distributeFlag, scheduleStatus);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 管理中心-设计订单派单
     *
     * @param distributeDesignReq
     * @return
     */
    @RequestMapping(value = "/distribute", method = RequestMethod.PUT)
    @ResponseBody
    public Map distributeDesignOrder(@RequestBody DistributeDesignReq distributeDesignReq) {
        int result = this.designOrderService.distributeDesignOrder(distributeDesignReq);
        if (result == 1) {
            try {
                String supervisorMobile = designOrderService.getMobileByUserId(distributeDesignReq.getSupervisorId());
                SmsKits.send(SmsContentConfig.maintainRemind, supervisorMobile);
                String managerMobile = designOrderService.getMobileByUserId(distributeDesignReq.getManagerId());
                SmsKits.send(SmsContentConfig.maintainRemind, managerMobile);
                String userMobile = designOrderService.getMobileByUserId(distributeDesignReq.getUserId());
                SmsKits.send(SmsContentConfig.maintainRemind, userMobile);
            } catch (Exception e) {
                System.out.print("短信发送失败");
            }
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "派单成功");
        }
        return UtilReturn.resErrorMap("派单失败", UtilReturn.ERROR_CODE);
    }

    /**
     * 删除设计订单
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteDesign(@RequestParam("orderId") String orderId) {
        int result = this.designOrderService.deleteDesign(orderId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败", UtilReturn.ERROR_CODE);
    }


    /**
     * 设计订单 - 人员详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/relatedstuff", method = RequestMethod.GET)
    @ResponseBody
    public Map getRelatedStuff(@RequestParam("orderId") String orderId) {
        Map<String, Object> json = new HashMap<String, Object>();
        //获取相关人员id
        RelatedStuffResp relatedStuffResp = designOrderService.getRelatedStuff(orderId);
        //获取设计师信息
        RelatedStuffInfo designerInfo = designOrderService.getStuffInfoById(relatedStuffResp.getUserId());
        //获取监理信息
        RelatedStuffInfo supervisorInfo = designOrderService.getStuffInfoById(relatedStuffResp.getSupervisorId());
        //获取项目经理信息
        RelatedStuffInfo managerInfo = designOrderService.getStuffInfoById(relatedStuffResp.getManagerId());
        json.put("designer", designerInfo);
        json.put("supervisor", supervisorInfo);
        json.put("manager", managerInfo);
        return json;
    }

    /**
     * 获取测量数据
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/measuredata", method = RequestMethod.GET)
    @ResponseBody
    public Map getMeasureData(@RequestParam("orderId") String orderId) {
        MeasureCenterReq measureCenterReq = iDesignCenterService.getOrderInfo(orderId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureCenterReq);
    }

    /**
     * 设计订单-方案详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/orderdata", method = RequestMethod.GET)
    @ResponseBody
    public Map getMeasureData(@RequestParam("id") Integer id) {
        OrderDesignDetail orderDesignDetail = iDesignCenterService.getOrderDataDetail(id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, orderDesignDetail);
    }

}
